package com.xyh.student.service.impl;


import com.xyh.student.utils.StringUtil;
import com.xyh.student.dao.TeacherDao;
import com.xyh.student.entity.ResponseData;
import com.xyh.student.entity.Teacher;
import com.xyh.student.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao  teacherDao;

    @Override
    public ResponseData queryAllTeacher(Integer pageNum,Integer pageSize) {
        try{
            List<Teacher> teacher=this.teacherDao.queryAllTeacher(pageNum,pageSize);
            return new ResponseData("0","请求成功",teacher);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }

    }

    @Override
    public ResponseData queryByid(Integer id) {
        try{
            List<Teacher> teacher=this.teacherDao.queryByid(id);
            return new ResponseData("0","请求成功",teacher);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData insertTeacher(Teacher teacher) {
        try{
            int i=this.teacherDao.insertTeacher(teacher);
            return new ResponseData("0","请求成功",i);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deleteTeacherByid(Integer id) {
        try{
            int d=this.teacherDao.deleteTeacherByid(id);
            return new ResponseData("0","请求成功",d);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData deletes(int[] ids) {
        try{
            int d=this.teacherDao.deletes(ids);
            return new ResponseData("0","请求成功",d);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData updateTeacher(Teacher teacher) {
        try{
           int u=this.teacherDao.updateTeacher(teacher);
            return new ResponseData("0","请求成功",u);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData numberOfTeachers() {
        try{
           int n=this.teacherDao.numberOfTeachers();
            return new ResponseData("0","请求成功",n);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData userRegister(Teacher user) {
        String phone = user.getPhone();
        String username = user.getName();
        String password = user.getPassword();
//对用户 输入的用户名 手机号 密码进行非空校验 非空校验 phone取出空格 只要一个代码两次 提取
        if(StringUtil.isNull(phone)){
            return new ResponseData("9001","手机号为空");
        }
        if(StringUtil.isNull(password)){
            return new ResponseData("9002","密码为空");
        }
        if(StringUtil.isNull(username)){
            return new ResponseData("9003","参数为空");
        }
        try{
//校验用户名是否存在
//如何校验？ 根据username查询user表 如果有数据 说明存在 返回提示信息
            Teacher queryUser = (Teacher) teacherDao.queryUserByUserName(username);
            if(queryUser!=null){
                return new ResponseData("9004","用户名已经存在");
            }

//保存
            user.setPassword(password);

            teacherDao.insertTeacher(user);
            return new ResponseData("0","请求成功");
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("9999","网络异常");
        }
    }

    @Override
    public ResponseData userLogin(String name, String password) {
        //1.非空校验
        if(StringUtil.isNull(name)){
            return new ResponseData("9001","用户名为空");
        }
        if(StringUtil.isNull(password)){
            return new ResponseData("9002","密码为空");
        }
//3.根据手机号密码查询用户信息 user
//调用dao层的根据手机号和密码查询的方法进行查询
        Teacher user = (Teacher) teacherDao.queryUserBypassword(password);
//            如果user为null证明用户登录失败，需要返回给前端错误信息
//            如果user不为null需要继续进行后续的业务逻辑操作
        if(user==null){ //说明错误
            return new ResponseData("9005","密码不匹配");
        }
        try{
            return new ResponseData("0","请求成功");
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("9999","网络异常");
        }
    }

    @Override
    public ResponseData queryUserBypassword(String password) {
        try{
            List<Teacher> teacher=this.teacherDao.queryUserBypassword(password);
            return new ResponseData("0","请求成功",teacher);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }

    @Override
    public ResponseData queryUserByUserName(String name) {
        try{
            name= "%"+name+"%";
            List<Teacher> teacher=this.teacherDao.queryUserByUserName(name);
            return new ResponseData("0","请求成功",teacher);
        }catch (Exception e){
            System.out.println(e);
            return new ResponseData("888","请求失败");
        }
    }
}
